Page({
    data: {
      activeTab: 'shop',        // 当前 Tab
      shopList: [],             // 店铺列表
      goodsList: [],            // 商品列表
      shopPage: 1,
      goodsPage: 1,
      shopNoMore: false,
      goodsNoMore: false,
      shopRefresher: false,
      goodsRefresher: false
    },

    onLoad() {
      // 使用模拟数据
      this.generateMockShopData();
      this.generateMockGoodsData();
    },

    /* 切换 Tab */
    switchTab(e) {
      const tab = e.currentTarget.dataset.tab;
      if (tab === this.data.activeTab) return;
      this.setData({ activeTab: tab });
    },

    /* 生成模拟店铺数据 */
    generateMockShopData() {
      const shopList = [];
      const shopNames = ['精品生活超市', '数码电子商城', '时尚服饰店', '美食天地', '家居生活馆'];
      const logos = [
        'https://via.placeholder.com/100x100?text=超市',
        'https://via.placeholder.com/100x100?text=数码',
        'https://via.placeholder.com/100x100?text=服饰',
        'https://via.placeholder.com/100x100?text=美食',
        'https://via.placeholder.com/100x100?text=家居'
      ];

      for (let i = 1; i <= 10; i++) {
        const index = Math.floor(Math.random() * shopNames.length);
        shopList.push({
          id: i,
          name: shopNames[index],
          logo: logos[index],
          fans: Math.floor(Math.random() * 10000) + 1000,
          rating: (4 + Math.random() * 1).toFixed(1),
          goodsCount: Math.floor(Math.random() * 100) + 10
        });
      }

      this.setData({
        shopList,
        shopNoMore: true
      });
    },

    /* 生成模拟商品数据 */
    generateMockGoodsData() {
      const goodsList = [];
      const goodsTitles = [
        '新款智能手机', '无线蓝牙耳机', '时尚休闲运动鞋', '高清数字电视', '舒适纯棉T恤',
        '智能手表', '便携式笔记本电脑', '高端咖啡机', '精美项链', '专业相机'
      ];

      for (let i = 1; i <= 20; i++) {
        const index = Math.floor(Math.random() * goodsTitles.length);
        const price = Math.floor(Math.random() * 9999) + 100;
        goodsList.push({
          id: i,
          title: goodsTitles[index],
          pic: `https://via.placeholder.com/300x300?text=商品${i}`,
          price: price,
          originalPrice: price + Math.floor(Math.random() * 500),
          sales: Math.floor(Math.random() * 1000),
          rating: (4 + Math.random() * 1).toFixed(1)
        });
      }

      this.setData({
        goodsList,
        goodsNoMore: true
      });
    },

    /* 下拉刷新 */
    onShopRefresh() {
      this.setData({ shopRefresher: true });
      setTimeout(() => {
        this.generateMockShopData();
        this.setData({ shopRefresher: false });
      }, 1000);
    },
    onGoodsRefresh() {
      this.setData({ goodsRefresher: true });
      setTimeout(() => {
        this.generateMockGoodsData();
        this.setData({ goodsRefresher: false });
      }, 1000);
    },

    /* 上拉加载 */
    onReachBottom() {
      // 模拟加载更多
      if (this.data.activeTab === 'shop' && !this.data.shopNoMore) {
        setTimeout(() => {
          this.generateMockShopData();
        }, 1000);
      }
      if (this.data.activeTab === 'goods' && !this.data.goodsNoMore) {
        setTimeout(() => {
          this.generateMockGoodsData();
        }, 1000);
      }
    },

    /* 跳转到店铺详情 */
    goToShopDetail(e) {
      const id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: `/pages/merchant/merchant?id=${id}`
      });
    },

    /* 跳转到商品详情 */
    goToGoodsDetail(e) {
      const id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: `/pages/gooddetail/gooddetail?id=${id}`
      });
    }
  });